package com.nlgc365.designpattern.Chapter04_Pattern.Creation.P01_Singleton.Usage.Logger;

import java.io.FileWriter;
import java.io.IOException;

public class Logger {
    private FileWriter fileWriter;

    public Logger() {
        String fileName = "/path/logFile.txt";
        try {
            //保证FilterWriter在写入"/path/logFile.txt"的时候, 不被多线程下的调用者彼此覆盖
            synchronized (Logger.class) {
                fileWriter = new FileWriter(fileName,true);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void log(String message) {
        try {
            fileWriter.write(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
